//
//  LocalViewController.m
//  MiaoTu
//
//  Created by dllo on 16/9/9.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "LocalViewController.h"
#import "ZWYNetTool.h"
#import "LocalHeaderView.h"
#import "ZFChooseTimeViewController.h"
#import "LocalTableCell.h"
#import "LocalModel.h"
#import "LocalItem.h"
#import "UIView+Frame.h"
#import "MJRefresh.h"
#import "LocalDetailViewController.h"

#define kLocalTableCell @"LocalTableCellIdentifier"

@interface LocalViewController ()
<
    UITableViewDataSource,
    UITableViewDelegate
>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray<LocalItem *> *localArray;
@property (nonatomic, assign) NSInteger currentPage;
@end

@implementation LocalViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self createTableView];
    [self createData];

}

#pragma mark - View
- (void)createTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.dk_width, self.view.dk_height - 64 - 49) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self createTableHeaderView];
    [_tableView registerNib:[UINib nibWithNibName:@"LocalTableCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kLocalTableCell];
    _tableView.rowHeight = 322;
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(createData)];
    [self.tableView.mj_header beginRefreshing];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footRefreshData)];
}

- (void)createTableHeaderView {
    self.headerView = [LocalHeaderView headerView];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pushCalendar)];
    [_headerView.dateView addGestureRecognizer:tap];
    _tableView.tableHeaderView = _headerView;
}

- (void)pushCalendar {
    ZFChooseTimeViewController *calendarVC = [[ZFChooseTimeViewController alloc] init];
    
    __weak typeof(self)weekSelf = self;
    
    [calendarVC backDate:^(NSArray *goDate, NSArray *backDate) {
        weekSelf.headerView.startAndEndDate = [NSString stringWithFormat:@"%@:%@",[goDate componentsJoinedByString:@"-"],[backDate componentsJoinedByString:@"-"]];
    }];
    
    [self.navigationController pushViewController:calendarVC animated:YES];
}

#pragma mark - Data
- (void)createData {
    NSDictionary *headerDic = @{@"User-Agent":@"Dalvik/1.6.0 (Linux; U; Android 4.3; R6007 Build/JLS36C) Paros/3.2.13", @"Host":@"api.miaotu.net", @"Connection":@"Keep-Alive"};
    [ZWYNetTool GET:@"http://api.miaotu.net/v2/res/recommend?page=1&num=10" andBody:nil andHeader:headerDic andResponse:ZWYJSON andSuccessBlock:^(id result) {
        LocalModel *model = [LocalModel modelWithDic:result];
        self.localArray = model.Items.mutableCopy;
        [_tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        _currentPage = 1;
    } andFailureBlock:^(NSError *error) {
        
    }];
}

- (void)footRefreshData {
    _currentPage += 1;
    NSString *urlStr = [NSString stringWithFormat:@"http://api.miaotu.net/v2/res/recommend?page=%ld&num=10", (long)_currentPage];
    NSDictionary *headerDic = @{@"User-Agent":@"Dalvik/1.6.0 (Linux; U; Android 4.3; R6007 Build/JLS36C) Paros/3.2.13", @"Host":@"api.miaotu.net", @"Connection":@"Keep-Alive"};
    [ZWYNetTool GET:urlStr andBody:nil andHeader:headerDic andResponse:ZWYJSON andSuccessBlock:^(id result) {
        LocalModel *model = [LocalModel modelWithDic:result];
        NSArray<LocalItem *> *items = model.Items;
        for (LocalItem *item in items) {
            [_localArray addObject:item];
        }
        [_tableView reloadData];
        [self.tableView.mj_footer endRefreshing];
        
    } andFailureBlock:^(NSError *error) {
        
    }];
}

#pragma mark - TableView DataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _localArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LocalTableCell *cell = [tableView dequeueReusableCellWithIdentifier:kLocalTableCell forIndexPath:indexPath];
    cell.localItem = _localArray[indexPath.row];
    return cell;
}

#pragma mark - TableView Delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    LocalDetailViewController *localDetailVC = [[LocalDetailViewController alloc] init];
    localDetailVC.localItem = _localArray[indexPath.row];
    [self.navigationController pushViewController:localDetailVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
